#include "stdafx.h"
#include "bubble_redpacket.h"
#include "export/nim_ui_window_manager.h"
#include "module/session/session_manager.h"


using namespace ui;

namespace nim_comp
{
	void MsgBubbleRedPacket::InitControl(bool bubble_right)
	{
		__super::InitControl(bubble_right);
		msg_redpacket_ = new ButtonBox;
		GlobalManager::FillBoxWithCache(msg_redpacket_, L"session/redpacket.xml");
		bubble_box_->Add(msg_redpacket_);
		redpacket_ = FindSubControl(L"redpacket");
		msg_redpacket_->AttachClick(nbase::Bind(&MsgBubbleRedPacket::OnClicked, this, std::placeholders::_1));
		msg_redpacket_->AttachMenu(nbase::Bind(&MsgBubbleRedPacket::OnMenu, this, std::placeholders::_1));
		have_received_ = false;
	}

	void MsgBubbleRedPacket::InitInfo(const nim::IMMessage& msg)
	{
		__super::InitInfo(msg);
		Json::Value json;
		if (StringToJson(msg.attach_, json) && json.isObject())
		{
			types_ = json["types"].asInt();
			pack_id_ = json["pack_id"].asString();
			amount_type_ = json["amount_type"].asString();
			amount_type_name_ = json["amount_type_name"].asString();
			amount_ = json["amount"].asString();
			tid_ = types_ == 1 ? msg.receiver_accid_ : "";
			std::string content = json["content"].asString();
			// 检查是否已经点击过了
			Json::Value status;
			if (StringToJson(msg_.msg_setting_.local_ext_, status) && status.isObject())
			{
				have_received_ = true;
				redpacket_->SetBkColor(L"bk_redpacket_received");
			}
			ui::Label* redpacket_title = (Label*)FindSubControl(L"title");
			redpacket_title->SetText(nbase::UTF8ToUTF16(content).c_str());
			ui::Label* redpacket_type = (Label*)FindSubControl(L"type");
			redpacket_type->SetText(nbase::UTF8ToUTF16(amount_type_name_ + " 红包").c_str());
			return;
		}
		QLOG_WAR(L"user type msg undefine, attach={0}") << msg.attach_;
	}

	bool MsgBubbleRedPacket::OnClicked(ui::EventArgs* arg)
	{
		if (have_received_) {
			return true;
		}
		auto task = ToWeakCallback([this](int res, const std::map<std::string, std::string>& data) {
			Json::Value values;
			values["clicked"] = true;
			values["msg"] = data.at("msg");
			msg_.msg_setting_.local_ext_ = values.toStyledString();
			nim::MsgLog::UpdateLocalExtAsync(msg_.client_msg_id_, msg_.msg_setting_.local_ext_, nullptr);
			have_received_ = true;
			redpacket_->SetBkColor(L"bk_redpacket_received");
			if (res == 0) {
				std::wstring format = L"恭喜您获得了 %s %s币";
				std::wstring toast = nbase::StringPrintf(format.c_str(), nbase::UTF8ToUTF16(amount_).c_str(), nbase::UTF8ToUTF16(amount_type_name_).c_str());
				nim_ui::ShowToast(toast, 3000);
				// 添加提示消息
				SessionBox* session_form = SessionManager::GetInstance()->FindSessionBox(sid_);
				if (session_form) {
					nim::IMMessage msg;
					msg.session_type_ = type_;
					msg.receiver_accid_ = sid_;
					msg.sender_accid_ = LoginManager::GetInstance()->GetAccount();
					msg.client_msg_id_ = nim::Tool::GetUuid();
					msg.msg_setting_.resend_flag_ = BS_FALSE;
					msg.msg_setting_.need_push_ = BS_FALSE;
					msg.timetag_ = 1000 * nbase::Time::Now().ToTimeT();
					msg.status_ = nim::kNIMMsgLogStatusSending;
					msg.type_ = nim::kNIMMessageTypeTips;
					msg.content_ = nbase::UTF16ToUTF8(L"已领取红包");
					session_form->ShowMsg(msg, true);
				}
			} else {
				std::wstring toast = nbase::UTF8ToUTF16(data.at("msg")).c_str();
				nim_ui::ShowToast(toast, 2000);
			}
		});
		app_sdk::AppSDKInterface::GetInstance()->InvokeRecvRedPacket(pack_id_, tid_, task);
		return true;
	}

	bool MsgBubbleRedPacket::OnMenu(ui::EventArgs* arg)
	{
		PopupMenu(false, true, false);
		return false;
	}
}